package com.zoke.support.image;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zoke.support.R;
import com.zoke.support.base.BaseObjectListAdapter;
import com.zoke.support.config.ZokeConfig;
import com.zoke.support.image.activity.GalleryActivity.HandleType;
import com.zoke.support.image.activity.ShowImagesActivity;
import com.zoke.support.utils.MessageHandlerList;
import com.zoke.support.utils.ViewHolder;
import com.zoke.support.view.ZokeTextView;
import com.zoke.support.view.ZokeToast;

/**
 * 查看相册列表
 * 
 * @author JackWu
 * @version
 * @date 2014-11-19
 * @_QQ_ 651319154
 */
public class ShowImagesAdapter extends BaseObjectListAdapter {
	/** 监听adapter点击，在UI内执行跳转的逻辑 **/
	public interface AdaterListener {
		void onAdapterItemClick(int position, Object obj);
	}

	private AdaterListener mListen;

	private int mType;
	private int mMax;

	private int selectTotal = 0;// 共选择多少张
//	private static final int MUST_PHOTO = 9;// 最多支持9张

	public ShowImagesAdapter(Context context, List<? extends Object> datas) {
		super(context, datas);
	}

	public ShowImagesAdapter(Context context, List<? extends Object> datas,
			int type, AdaterListener listen,int max) {
		super(context, datas);
		this.mType = type;
		this.mListen = listen;
		this.mMax = max;
	}

	@Override
	public View getItemView(final int position, View convertView,
			ViewGroup parent) {
		convertView = getConvertView(convertView,
				R.layout.grideview_item_showimages);
		final ShowImageBeen been = (ShowImageBeen) mDatas.get(position);
		final ImageView mImageIv = ViewHolder.get(convertView, R.id._image);
		final ImageView mSelectedIv = ViewHolder.get(convertView,
				R.id._isselected);
		final ZokeTextView mSeTv = ViewHolder.get(convertView,
				R.id.item_image_grid_text);

		if (been.isChecked()) {
			mSelectedIv.setImageResource(R.drawable.photo_seletede);
			mSeTv.setBackgroundResource(R.drawable.image_selected_line);
		} else {
			mSelectedIv.setImageResource(-1);
			mSeTv.setBackgroundColor(0x00000000);
		}

		mImageIv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				handleType(position, been, mSelectedIv, mSeTv);
			}
		});
		String uri = "file:///" + been.getPath();
		ImageLoader.getInstance().displayImage(uri, mImageIv,
				ImageOptions.getThumbOptions());
		return convertView;
	}

	/** 处理更多图片操作的逻辑 **/
	private void handleMore(ShowImageBeen been, ImageView mSelectedIv,
			ZokeTextView mSeTv) {
		// 多张图片的业务逻辑
		if (selectTotal < mMax) {
			// 9张内的图片选择操作
			// 处理++ --的操作
			been.setChecked(!been.isChecked());
			if (been.isChecked()) {
				// 处理选中效果
				selectTotal++;
				mSelectedIv.setImageResource(R.drawable.photo_seletede);
				mSeTv.setBackgroundResource(R.drawable.image_selected_line);
				MessageHandlerList.sendMessage(ShowImagesActivity.class,
						ZokeConfig.UPDATEIMAGECOUNT, selectTotal);
				return;
			}

			if (!been.isChecked()) {
				selectTotal--;
				// 处理取消选中效果
				mSelectedIv.setImageResource(-1);
				mSeTv.setBackgroundColor(0x00000000);
				MessageHandlerList.sendMessage(ShowImagesActivity.class,
						ZokeConfig.UPDATEIMAGECOUNT, selectTotal);
				return;
			}
			return;
		}

		if (selectTotal >= mMax) {
			// 如果大于9张时，取消选中并--
			if (been.isChecked()) {
				been.setChecked(!been.isChecked());
				selectTotal--;
				// 处理取消选中效果
				mSelectedIv.setImageResource(-1);
				mSeTv.setBackgroundColor(0x00000000);
				MessageHandlerList.sendMessage(ShowImagesActivity.class,
						ZokeConfig.UPDATEIMAGECOUNT, selectTotal);
				return;
			}
			// 9张外的图片选择操作
			ZokeToast.show(mContext, "最多可选" + mMax + "张图片");
			return;
		}

	}

	/** 处理一张图片的操作 **/
	private void handleOne(int position, ShowImageBeen been) {
		if (mListen != null)
			mListen.onAdapterItemClick(position, been);
	}

	/** 处理裁剪操作 **/
	private void handleCrop(int position, ShowImageBeen been) {
		if (mListen != null)
			mListen.onAdapterItemClick(position, been);
	}

	/**
	 * 处理不同的点击事件
	 * 
	 * @param beeb
	 * @method
	 */
	private void handleType(int position, ShowImageBeen been,
			ImageView mSelectedIv, ZokeTextView mSeTv) {
		if (mType == HandleType.CROP) {
			// 裁剪图片的业务逻辑
			handleCrop(position, been);
			return;
		}

		if (mType == HandleType.ONE) {
			// 一张图片的业务逻辑
			handleOne(position, been);
			return;
		}

		if (mType == HandleType.MORE) {
			handleMore(been, mSelectedIv, mSeTv);
			return;
		}

		if (mType == HandleType.FILTER) {
			// 单张并加效果
			return;
		}
	}
}
